package com.bfly.mall.advert.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.advert.entity.Advert;
import com.bfly.mall.advert.dto.AdvertFormDTO;
import com.bfly.mall.advert.service.IAdvertService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/18 22:41
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class AdvertServiceImpl extends BaseServiceImpl<Advert, Integer> implements IAdvertService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AdvertFormDTO formDTO) {
        Advert advert = new Advert();
        BeanUtils.copyProperties(formDTO, advert);

        advert.setId(0);
        advert.setCreateDate(new Date());
        advert.setUpdateDate(new Date());
        super.save(advert);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(AdvertFormDTO formDTO) {
        Advert advert = get(formDTO.getId());
        if(advert==null){
            throw new ServiceResponseException(SysError.PARAM_ERROR,"不存在的广告信息!");
        }
        BeanUtils.copyProperties(formDTO, advert);

        advert.setUpdateDate(new Date());
        super.edit(advert);
    }
}
